
package com.insigma.business.qrcode.common.converter;



import com.insigma.business.qrcode.soldier.dto.PlacementSoldierInfoBSearcher;
import com.insigma.business.qrcode.soldier.dto.PlacementSoldierInfoDTO;
import com.insigma.business.qrcode.soldier.dto.PlacementSoldierInfoInsertRequest;
import com.insigma.business.qrcode.soldier.dto.PlacementSoldierInfoUpdateRequest;
import com.insigma.business.qrcode.soldier.entity.PlacementSoldierInfoDO;
import com.insigma.business.qrcode.soldier.service.PlacementSoldierInfoSearchRequest;
import org.springframework.beans.BeanUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class PlacementSoldierInfoProviderConverter extends AbstractConverter<PlacementSoldierInfoDTO, PlacementSoldierInfoDO> {

    private final static PlacementSoldierInfoProviderConverter placementSoldierInfoConverter = new PlacementSoldierInfoProviderConverter();

    private PlacementSoldierInfoProviderConverter(){}

    
    public static PlacementSoldierInfoBSearcher convertRequestSO2Searcher(final PlacementSoldierInfoSearchRequest request){
        final PlacementSoldierInfoBSearcher placementSoldierInfoBSearcher = new PlacementSoldierInfoBSearcher(request.getPageNo(),
        request.getPageSize(),request.getSortKeyValueJoint());
        BeanUtils.copyProperties(request,placementSoldierInfoBSearcher);
        return placementSoldierInfoBSearcher;
    }

    
    public static PlacementSoldierInfoDO convertRequestUO2DO(final PlacementSoldierInfoUpdateRequest placementSoldierInfoUpdateRequest) {
        final PlacementSoldierInfoDO placementSoldierInfoDO = new PlacementSoldierInfoDO();
        BeanUtils.copyProperties(placementSoldierInfoUpdateRequest,placementSoldierInfoDO);
        
        return placementSoldierInfoDO;
    }

    
    public static PlacementSoldierInfoDO convertRequestIO2DO(final PlacementSoldierInfoInsertRequest placementSoldierInfoInsertRequest) {
        final PlacementSoldierInfoDO placementSoldierInfoDO = new PlacementSoldierInfoDO();
        BeanUtils.copyProperties(placementSoldierInfoInsertRequest,placementSoldierInfoDO);
        return placementSoldierInfoDO;
    }

    
    public static List<PlacementSoldierInfoDTO> convertResponseDOList2DTOList(final List<PlacementSoldierInfoDO> placementSoldierInfoDOList){
        return placementSoldierInfoConverter.convertGenericResponseDOList2DTOList(placementSoldierInfoDOList);
    }

    
    public static PlacementSoldierInfoDTO convertResponseDO2DTO(final PlacementSoldierInfoDO placementSoldierInfoDO){
        return placementSoldierInfoConverter.convertGenericResponseDO2DTO(placementSoldierInfoDO);
    }

    public static void sortListByScore(List<PlacementSoldierInfoDTO> soldiers) {
        Collections.sort(soldiers, new Comparator<PlacementSoldierInfoDTO>() {
            @Override
            public int compare(PlacementSoldierInfoDTO o1, PlacementSoldierInfoDTO o2) {
                int i = o2.getTotalScore().compareTo(o1.getTotalScore());
                if (i != 0) {
                    return i;
                }
                int ac = o2.getArchiveScore().compareTo(o1.getArchiveScore());
                if (ac == 0) {
                    o1.setTiedFlag(true);
                    o2.setTiedFlag(true);
                }
                return i;
            }
        });
    }

    
    @Override
    protected PlacementSoldierInfoDTO doConvertResponseDO2DTO(PlacementSoldierInfoDO placementSoldierInfoDO) {
        final PlacementSoldierInfoDTO placementSoldierInfoDTO = new PlacementSoldierInfoDTO();
        try {
            BeanUtils.copyProperties(placementSoldierInfoDO, placementSoldierInfoDTO);
        }catch(RuntimeException be){
            
            return null;
        }
        return placementSoldierInfoDTO;
    }

}
